package com.flydog.taskManager.service.impl;

import com.flydog.taskManager.entity.PeriodicTask;
import com.flydog.taskManager.entity.PhasedTask;
import com.flydog.taskManager.entity.Task;
import com.flydog.taskManager.service.PeriodicTaskService;
import com.flydog.taskManager.service.PhasedTaskService;
import com.flydog.taskManager.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@Slf4j
public class TaskServiceImpl implements TaskService {

    @Autowired
    PhasedTaskService phasedTaskService;

    @Autowired
    PeriodicTaskService periodicTaskService;

    @Override
    public List<? extends Task> listEndIn(Calendar begin, Calendar end) {
        List<PhasedTask> phasedTaskList = phasedTaskService.listByEndDate(begin, end);
        List<PeriodicTask> periodicTaskList = periodicTaskService.listPeriodEndBetween(begin, end);
        Stream<Task> periodTaskStream = periodicTaskList.stream().map(periodicTask -> (Task) periodicTask);
        Stream<Task> phaseTaskStream = phasedTaskList.stream().map(phasedTask -> (Task) phasedTask);
        return Stream.concat(periodTaskStream, phaseTaskStream).collect(Collectors.toList());
    }

    @Override
    public List<? extends Task> listBeginIn(Calendar begin, Calendar end) {
        return phasedTaskService.listByBeginDate(begin, end);
    }

    @Override
    public List<? extends Task> refreshTaskStatusBeforeData(Calendar date) {
        List<? extends Task> changedPhaseTask = phasedTaskService.setStatusBeforeData(0, date);
        List<? extends Task> changedPeriodTask = periodicTaskService.refreshStatus(date);
        Stream<Task> periodTaskStream = changedPeriodTask.stream().map(periodicTask -> (Task) periodicTask);
        Stream<Task> phaseTaskStream = changedPhaseTask.stream().map(phasedTask -> (Task) phasedTask);
        return Stream.concat(periodTaskStream, phaseTaskStream).collect(Collectors.toList());
    }
}
